<template>
  <div class="login-container">
    <div class="login-background">
      <div class="login-diagonal"></div>
    </div>
    <div class="login-form-container">
      <div class="login-header">
        <h1>MES制造执行系统原型</h1>
        <h2>Manufacturing Execution System prototype</h2>
      </div>

      <el-form
        class="login-form"
        :model="loginForm"
        :rules="loginRules"
        ref="loginFormRef"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            prefix-icon="User"
            clearable
          ></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="Lock"
            show-password
            clearable
            @keyup.enter="handleLogin"
          ></el-input>
        </el-form-item>

        <el-form-item class="form-actions">
          <el-checkbox v-model="loginForm.remember">记住密码</el-checkbox>
          <a
            href="#"
            class="forgot-password"
            @click.prevent="handleForgotPassword"
            >忘记密码?</a
          >
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            class="login-btn"
            @click="handleLogin"
            :loading="loading"
          >
            登录
          </el-button>
        </el-form-item>

        <div class="register-prompt">
          还没有账号?
          <a href="#" class="register-link" @click.prevent="handleRegister"
            >立即注册</a
          >
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { useRouter } from "vue-router";
import { ElMessage, ElNotification } from "element-plus";
import { ArrowLeft, User, Lock } from "@element-plus/icons-vue";
import { postLoginApi } from "@/api/Login";
import { GET_TOKEN, SET_TOKEN } from "@/utils/token";
import useUserStore from "@/store/modules/User";
import { getTime } from "@/utils/time";

const router = useRouter();
const userStore = useUserStore();
// 登录表单数据
const loginForm = ref({
  username: "",
  password: "",
  remember: false,
});

// 表单验证规则
const loginRules = {
  username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
};

const loading = ref(false);
const loginFormRef = ref();
let message = getTime();

// 处理登录
const handleLogin = () => {
  loginFormRef.value.validate(async (valid: boolean) => {
    if (valid) {
      try {
        loading.value = true;
        const res: any = await postLoginApi({
          username: loginForm.value.username,
          password: loginForm.value.password,
        });
        if (res.code !== 210) {
          ElNotification({
            type: "success",
            message: "欢迎回来",
            title: `Hi，${message}好！`,
          });
          SET_TOKEN(res.sessionToken);
          userStore.token = GET_TOKEN();
          userStore.objectId = res.objectId;
          userStore.email = res.email;
          userStore.username = res.username;
          router.push("/home");
        } else {
          ElMessage.error("用户名或密码错误");
        }
      } catch (err: any) {
        ElMessage.error("登陆失败");
      } finally {
        loading.value = false;
      }
    }
  });
};

//去注册页面
const handleRegister = () => {
  router.push("/register");
};
//忘记密码
const handleForgotPassword = () => {
  console.log("点击忘记密码"); // 先确认事件是否触发
  router.push("/forgetPwd").catch((err) => {
    console.error("路由跳转失败:", err);
    ElMessage.error("无法打开忘记密码页面");
  });
};
</script>

<style scoped>
.login-container {
  display: flex;
  height: 100vh;
  width: 100vw;
  overflow: hidden;
}

.login-background {
  flex: 1;
  background: url("https://example.com/mes-background.jpg") no-repeat center
    center;
  background-size: cover;
  position: relative;
}

.login-diagonal {
  position: absolute;
  top: 0;
  right: 0;
  width: 200%;
  height: 200%;
  background: linear-gradient(
    to bottom left,
    transparent 0%,
    transparent 50%,
    rgba(27, 154, 238, 0.8) 50%,
    rgba(27, 154, 238, 0.8) 100%
  );
  transform: translateX(50%) translateY(-50%) rotate(45deg);
  transform-origin: center center;
  z-index: 1;
}

.login-form-container {
  width: 550px;
  background: white;
  padding: 60px 40px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  box-shadow: -5px 0 15px rgba(0, 0, 0, 0.1);
  z-index: 2;
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.login-header h1 {
  font-size: 24px;
  color: #1b9aee;
  margin-bottom: 10px;
}

.login-header h2 {
  font-size: 16px;
  color: #666;
  font-weight: normal;
}

.login-form {
  width: 100%;
}

.login-btn {
  width: 100%;
  background-color: #1b9aee;
  border-color: #1b9aee;
}

.login-btn:hover {
  background-color: #1b9aee;
  border-color: #1b9aee;
}

.login-footer {
  margin-top: 30px;
  text-align: center;
}

.copyright {
  color: #999;
  font-size: 12px;
}

/* 表单容器样式 */
.login-form {
  width: 100%;
  padding: 20px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}

/* 输入框样式 */
.login-form :deep(.el-input__wrapper) {
  height: 48px;
  border-radius: 6px;
  border: 1px solid #e0e0e0;
  transition: all 0.3s;
}

.login-form :deep(.el-input__wrapper.is-focus) {
  border-color: #1b9aee;
  box-shadow: 0 0 0 2px rgba(23, 92, 180, 0.2);
}

.login-form :deep(.el-input__inner) {
  font-size: 14px;
  color: #333;
}

.login-form :deep(.el-input__prefix) {
  color: #1b9aee;
  margin-left: 10px;
}

/* 记住密码和忘记密码区域 */
.form-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.forgot-password {
  color: #666;
  font-size: 14px;
  margin-left: 250px;
  text-decoration: none;
}

.forgot-password:hover {
  color: #1b9aee;
  text-decoration: underline;
}

/* 登录按钮样式 */
.login-btn {
  width: 100%;
  height: 48px;
  font-size: 16px;
  background-color: #1b9aee;
  border-color: #1b9aee;
  border-radius: 6px;
  transition: all 0.3s;
}

.login-btn:hover {
  background-color: #1a6fd8;
  border-color: #1a6fd8;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(23, 92, 180, 0.2);
}

.login-btn:active {
  transform: translateY(0);
}

/* 注册提示 */
.register-prompt {
  text-align: center;
  margin-top: 20px;
  color: #666;
  font-size: 14px;
}

.register-link {
  color: #1b9aee;
  text-decoration: none;
  margin-left: 5px;
}

.register-link:hover {
  text-decoration: underline;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-form {
    padding: 15px;
  }

  .login-form :deep(.el-input__wrapper) {
    height: 44px;
  }

  .login-btn {
    height: 44px;
    font-size: 15px;
  }

  .form-actions {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
  }
}
</style>
